package com.lzk.auth.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzk.auth.service.DeviceService;
import com.lzk.auth.service.DeviceStatusService;
import com.lzk.auth.service.FactoryService;
import com.lzk.auth.service.TypeService;
import com.lzk.common.result.Result;
import com.lzk.model.system.*;
import com.lzk.model.system.SysDevice;
import com.lzk.vo.system.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2023-12-29
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/admin/system/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceStatusService deviceStatusService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private FactoryService factoryService;


    @ApiOperation("设备分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        DeviceVo deviceVo
    ) {
        Page<SysDevice> pageParam = new Page<>(page, limit);

        LambdaQueryWrapper<SysDevice> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(deviceVo)){
            System.out.println(deviceVo);
        }else{
            System.out.println("Vo is null");
        }

        Integer dStatus = deviceVo.getDstatus();
        if (null != dStatus) {
            wrapper.eq(SysDevice::getDStatus, dStatus);
            System.out.println("-----------------");
            System.out.println(dStatus);
        }
        Integer dType = deviceVo.getDtype();
        if (null != dType){
            wrapper.eq(SysDevice::getDType, dType);
            System.out.println("-----------------");
            System.out.println(dType);
        }

        IPage<SysDevice> pageModel = deviceService.page(pageParam, wrapper);
        List<SysDevice> records = pageModel.getRecords();
        for (SysDevice record : records) {
            Integer status = record.getDStatus();
            Integer dType1 = record.getDType();
            SysDeviceStatus deviceStatus = deviceStatusService.getById(status);
            SysType type = typeService.getById(dType1);
            record.setRoleName(type.getName());
            record.setStatus(deviceStatus.getStatusName());
        }
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据id查设备")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysDevice device = deviceService.getById(id);
        return Result.ok(device);
    }
    @ApiOperation(value = "录入设备信息")
    @PostMapping("save")
    public Result save(@RequestBody SysDevice device) {
        deviceService.save(device);
        Integer dType = device.getDType();
        String incrementSql="device_count=device_count+1";
        UpdateWrapper<SysFactory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("factory_type", dType); // 根据ID作为更新条件
        updateWrapper.setSql(incrementSql);
        factoryService.update(updateWrapper);
        return Result.ok();
    }

    @ApiOperation(value = "更新设备信息")
    @PutMapping("update")
    public Result updateById(@RequestBody SysDevice device) {
        deviceService.updateById(device);
        return Result.ok();
    }

    @ApiOperation(value = "删除设备")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {

        UpdateWrapper<SysDevice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 根据ID作为更新条件

        updateWrapper.set("is_deleted", 1);
        deviceService.update(updateWrapper);

        return Result.ok();
    }


}

